package de.gwtprime.client.widgets.trading.popup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.event.OrderProcessedEvent;
import de.gwtprime.client.util.GWTUtils;
import de.gwtprime.shared.requestFactory.OrderProxy;
import de.gwtprime.shared.requestFactory.OrderProxy.OrderType;
import de.gwtprime.shared.requestFactory.User2Proxy;
import de.gwtprime.shared.requestFactory.request.OrderRequest;
import de.gwtprime.shared.util.AppUtilsShared;

public class OrderCreationConfirmationPopup extends PopupPanel {

	private static OrderCreationConfirmationPopupUiBinder uiBinder = GWT
			.create(OrderCreationConfirmationPopupUiBinder.class);

	interface OrderCreationConfirmationPopupUiBinder extends
			UiBinder<Widget, OrderCreationConfirmationPopup> {
	}

	@UiField HTML confirmText;
	@UiField Button confirmBtn;
	@UiField Button cancelBtn;
	
	@UiField CheckBox usePrivateAddressCheck;
	@UiField Label privateAddressVal;
	
	
	private ClientFactory clientFactory; 
	private OrderProxy order;
	private OrderRequest request;
	
	
	public OrderCreationConfirmationPopup(ClientFactory clientFactory, OrderProxy order, OrderRequest request) {
		super(true, true); // auto hide and modal
		setWidget(uiBinder.createAndBindUi(this));
		
		confirmText.setHTML(GWTUtils.I18n.confirmOrderText(order.getAmount().toString(), order.getPrice().toString(), (order.getType()==OrderType.BUY ? "kaufen" : "verkaufen")));
		
		this.clientFactory = clientFactory;
		this.order = order;
		this.request = request;
		usePrivateAddressCheck.setText("Use private address");
		
		if(order.getType()==OrderType.BUY){
			privateAddressVal.setText(clientFactory.getUser().getPrivateWithdrawAddress());
			privateAddressVal.setVisible(false);
			if(AppUtilsShared.isNullOrEmpty(clientFactory.getUser().getPrivateWithdrawAddress()))
				usePrivateAddressCheck.setVisible(false);
		}else{
			privateAddressVal.setVisible(false);
			usePrivateAddressCheck.setVisible(false);
		}
	}
	
	@UiHandler("confirmBtn")
	public void onConfirm(ClickEvent e){
		order.setPrivateWithdrawAddress(clientFactory.getUser().getPrivateWithdrawAddress());
		request.createNewOrder().using(order).fire(new Receiver<Long>() {

			@Override
			public void onSuccess(Long response) {
				
				clientFactory.getNotificationCenter().showMessage("Auftrag hinzugefügt");
				clientFactory.getEventBus().fireEvent(new OrderProcessedEvent(order));
				
				// Reload the user to get new values for balance etc.
				clientFactory.getRequestFactory().userRequest().findUser2(clientFactory.getUser().getId()).fire(new Receiver<User2Proxy>() {

					@Override
					public void onSuccess(User2Proxy user) {
						clientFactory.setUser(user);
						
						hide();
					}
				});
			}
		});
	}
	
	@UiHandler("usePrivateAddressCheck")
	public void onCheckBoxChange(ClickEvent e){
		privateAddressVal.setVisible(usePrivateAddressCheck.getValue());
	}
	
	@UiHandler("cancelBtn")
	public void onCancel(ClickEvent e){
		hide();
	}

}
